import {ChangeDetectorRef, Component, ViewChild} from '@angular/core';
import {App, NavController, Slides, ToastController} from 'ionic-angular';
import {KepuPage} from "../kepu/kepu";
import {BozhulistPage} from "../bozhulist/bozhulist";
import {HuodongPage} from "../huodong/huodong";
import {ZuopinPage} from "../zuopin/zuopin";
import {ZuopininfoPage} from "../zuopininfo/zuopininfo";
import {JidihttpProvider} from "../../providers/jidihttp/jidihttp";
import {BaikelistPage} from "../baikelist/baikelist";
import {HuodonginfoPage} from "../huodonginfo/huodonginfo";
import {KepujidiinfoPage} from "../kepujidiinfo/kepujidiinfo";
import {BozhuinfoPage} from "../bozhuinfo/bozhuinfo";
import {DomSanitizer} from "@angular/platform-browser";

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  /** banner **/
  pageno:number=1;
  /** zuopin **/
  bannerlistitem:Array<any>=[];//list列表
  bannerMoviesvaike: Array<any>=[];
   /** 提示信息 **/
  homeToast:string;
  /** 列表 基地 **/
  jidiMovies: Array<any>=[];
  tasklist:Array<any>=[];//list列表
  /** bozhu **/
  jidiMoviesbozhu: Array<any>=[];
  /** 列表 基地 **/
  tasklistbozhu:Array<any>=[];//list列表
  /** 百科讲堂 **/
  tasklistbaike:Array<any>=[];//list列表
  jidiMoviesvaike: Array<any>=[];
  /** 活动信息 **/
  tasklisthuodong:Array<any>=[];//list列表
  huodongMoviesvaike: Array<any>=[];
  /** zuopin **/
  zuopinlistitem:Array<any>=[];//list列表
  zuopinMoviesvaike: Array<any>=[];

  @ViewChild('slides') slides: Slides;

  constructor(public sanitizer:DomSanitizer,public navCtrl: NavController,private app:App,public jidiProvider:JidihttpProvider,public cdchange:ChangeDetectorRef,public toastCtrl: ToastController) {
     /** banner **/
    this.getBannerClient();
    /**科普基地 **/
    this.jidihttpClinet();
     /** 博主请求 **/
     this.bozhuhttpClinet();
     /** 百科讲堂 **/
     this.baikejiangtang();
     /** 活动信息 **/
     this.huodongxinxi();
     /** 作品 **/
     this.zuopin();
     /** 调用微信 callback **/
     this.weixinCallback();
  }

  /** 科普基地 **/
  kepuEvent(){
    this.app.getRootNav().push(KepuPage);

  }

  /** 博主列表 **/
  bozhulistEvent(){
    this.app.getRootNav().push(BozhulistPage);
  }

  /** 百科讲堂 **/
  baikeEvent(){
    this.app.getRootNav().push(BaikelistPage);
  }

  /** 活动 **/
  huodongEvent(){
    this.app.getRootNav().push(HuodongPage);
  }



  /** 作品 **/
  zuopinEvent(){
    this.app.getRootNav().push(ZuopinPage);
  }
  huodongqishinEvent(){
    location.href="https://mp.weixin.qq.com/s/gRYrduZorT3swTE6Y46W2g";
  }

  /** 作品详情 **/
  zuopinInfoEvent(){
    this.app.getRootNav().push(ZuopininfoPage);
  }

  /** 作品请求 **/
  zuopin(){
    this.jidiProvider.getZuoPin(this.pageno,"create_time","first").subscribe(data => {
      let leng=data['data']['records']['length'];
      if(leng>0){
        for(let i=0;i<leng;i++){
          this.zuopinMoviesvaike.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);data['data']['records']['showType']
        this.zuopinlistitem=this.zuopinMoviesvaike;
        this.cdchange.detectChanges();
      }
    },error => {

    });

  }


  /** 基地请求 http **/
  jidihttpClinet() {
    //let tasklist = this.jidiProvider.jidiMovies;
    this.jidiProvider.getHotjidi(this.pageno).subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<data['data']['records']['length'];i++){
          this.jidiMovies.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.tasklist=this.jidiMovies;
        this.cdchange.detectChanges();
      }
    },error => {

    });

  }
  /** 博主请求 **/
  bozhuhttpClinet(){
    this.jidiProvider.getBozhuHttp(this.pageno).subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<data['data']['records']['length'];i++){
          this.jidiMoviesbozhu.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.tasklistbozhu=this.jidiMoviesbozhu;
        this.cdchange.detectChanges();
      }
    },error => {

    });


  }

  /** 百科讲堂 **/
  baikejiangtang(){

    this.jidiProvider.getBaikeJIangtangHttp(this.pageno).subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<1;i++){
          this.jidiMoviesvaike.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.tasklistbaike=this.jidiMoviesvaike;
        this.cdchange.detectChanges();
      }
    },error => {

    });


  }

  /** huodongxinxi **/
  huodongxinxi(){

    this.jidiProvider.getHuodongHttp(this.pageno).subscribe(data => {
      if(data['data']['records']['length']>0){
        for(let i=0;i<2;i++){
          this.huodongMoviesvaike.push(data['data']['records'][i]);
        }
        //alert(this.jidiMovies);
        this.tasklisthuodong=this.huodongMoviesvaike;
        this.cdchange.detectChanges();
      }
    },error => {

    });


  }

  /** weixin 是否登陆 **/
  weixinCallback(){
    this.jidiProvider.getLoginType().subscribe(data => {
      if(data['success']){
        this.showToast(data['data']);
        localStorage.setItem('login',"yes");
      }else{
        localStorage.setItem('login',"no");
        //this.app.getRootNav().push(LoginPage);
      }

    },error => {

    });

  }



  /** 提示信息 **/
  showToast(position: string) {
    let toast = this.toastCtrl.create({
      message: "您好，"+position,
      duration: 2000,
      position: "middle"
    });

    toast.present(toast);
  }


  /** banner http **/
  getBannerClient(){
    this.jidiProvider.getBannerHttp(this.pageno).subscribe(data => {
      if(data['success']){
        if(data['data']['records']['length']>0){
          for(let i=0;i<data['data']['records']['length'];i++){
            this.bannerMoviesvaike.push(data['data']['records'][i]);
          }
          //alert(this.jidiMovies);
          this.bannerlistitem=this.bannerMoviesvaike;
          this.cdchange.detectChanges();
        }

      }else{
        localStorage.setItem('login',"no");
        //this.app.getRootNav().push(LoginPage);
      }

    },error => {

    });
  }

  ionViewDidEnter() {
    if(this.slides){

      this.slides.autoplayDisableOnInteraction = false;
    }
  }

  /** 活动 **/
  querymore(uuid){
    //this.app.getRootNav().push(KepuPage);
    this.app.getRootNav().push(HuodonginfoPage,{uuid:uuid});
  }

  /** url **/
  urlEvent(url:string,type:string,iid:string){
    if(type=="activity"){//活动
      this.app.getRootNav().push(HuodonginfoPage,{uuid:iid});
    }else if(type=="worksDetail"){//作品
      this.app.getRootNav().push(ZuopininfoPage,{uuid:iid});
    }else if(type=="science"){//科普基地
      this.app.getRootNav().push(KepujidiinfoPage,{uuid:iid});
    }else if(type=="champion"){//博主
      this.app.getRootNav().push(BozhuinfoPage,{uuid:iid});
    } else{
      location.href=url;
    }

  }

  /** kepujidi **/
  querymorejidi(uuid){
    //this.app.getRootNav().push(KepuPage);
    this.app.getRootNav().push(KepujidiinfoPage,{uuid:uuid});
  }

  /** buzhu  **/
  querymorebozhu(uuid){
    this.app.getRootNav().push(BozhuinfoPage,{uuid:uuid});
  }

  assembleHTML(strHTML:any){
    return this.sanitizer.bypassSecurityTrustHtml(strHTML);
  }

}
